/**
 * 
 */
package com.wolfpire.proms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.proms.dao.ProjectIndexDao;
import com.wolfpire.proms.dao.TaskDao;
import com.wolfpire.proms.dao.TaskPlanDao;
import com.wolfpire.proms.model.ProjectIndex;
import com.wolfpire.proms.model.Task;
import com.wolfpire.proms.model.TaskPlan;
import com.wolfpire.proms.service.TaskService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.AttachmentService;
import com.wolfpire.workflow.dao.ProInstanceDao;
import com.wolfpire.workflow.em.OperateType;
import com.wolfpire.workflow.om.ProInstance;
import com.wolfpire.workflow.spring.WorkflowService;
import com.wolfpire.workflow.spring.impl.JbpmTaskService;

/**
 * @author lihd
 *
 */

@Service("taskService")
public class TaskServiceImpl extends BaseHibernateService<Task, Long> implements
		TaskService {
	
	@Resource private TaskDao taskDao;
	
	@Resource private TaskPlanDao taskPlanDao;
	
	@Resource private ProjectIndexDao projectIndexDao;
	
	@Resource private AttachmentService attachmentService;
	
	@Resource private WorkflowService workflowService;
	
	@Resource private JbpmTaskService jbpmTaskService;
	
	@Resource private ProInstanceDao proInstanceDao;

	@Override
	protected IBaseHibernateDao<Task, Long> getEntityDao() {
		return this.taskDao;
	}
	
	@Override
	public List<Task> list(Task entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<Task> setPageDataList(Page<Task> page, Task filterTask) {
		return this.taskDao.findTasks(page, filterTask);
	}

	@Override
	public void del(Long id) {
		Task task = this.taskDao.get(id);
		if (null != task) {
			task.setDelFlag(Constants.DELETE_FLAG);
			this.taskDao.saveOrUpdate(task);
		}
	}

	@Override
	public Task getProjectIndexInfoTask(Long id) {
		Task task = this.taskDao.get(id);
		if (null != task.getProjectIndexId()) {
			ProjectIndex projectIndex = this.projectIndexDao.get(task.getProjectIndexId());
			task.setProjectIndexName(projectIndex.getName());
		}
		return task;
	}

	@Override
	public void move(Long id, Long projectIndexId, Long targetProjectIndexId) {
		Task task = this.taskDao.get(id);
//		ProjectIndex targetProjectIndex = this.projectIndexDao.get(targetProjectIndexId);
		task.setProjectIndexId(targetProjectIndexId);
		this.taskDao.saveOrUpdate(task);
	}

	@Override
	public void saveOrUpdate(Task task) {
		this.taskDao.saveOrUpdate(task);
		Long taskId = task.getId();
		this.attachmentService.batchRelateAttachment(task.getAttId(), taskId, "task");
		Long[] taskPlanIds = task.getTaskPlanIds();
		if (ArrayUtils.isNotEmpty(taskPlanIds)) {
			for (Long taskPlanId : taskPlanIds) {
				TaskPlan taskPlan = this.taskPlanDao.get(taskPlanId);
				taskPlan.setTaskId(taskId);
				this.taskPlanDao.save(taskPlan);
			}
		}
	}

	@Override
	public Page<Map<String, Object>> setPageApplyDataList(Page<Map<String, Object>> page, Task filterTask) {
		String formTypeId = Constants.TASK_FORM_TYPE;
		return this.taskDao.findApplyTasks(page, filterTask, formTypeId);
	}

	@Override
	public void startTaskWorkflow(User user, Task task) {
		// TODO Auto-generated method stub
		task.setCreateUserId(user.getId());
		task.setCreateTime(new Date());
		task.setStatus(Constants.TASK_STATUS_APPLY);
		this.saveOrUpdate(task);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userIds", String.valueOf(user.getId()));
		ProInstance proInstance = this.workflowService.startProcess(Constants.TASK_WORKFLOW_ID, task.getId(), user, params);
		// 再提交至“任务申请”
		Map<String, Object> data = this.jbpmTaskService.getData(String.valueOf(proInstance.getInstanceId()));
		this.jbpmTaskService.subWorkFlowSingle(data.get("WF_DBID_").toString(), Constants.TASK_NODE_2, String.valueOf(user.getId()), StringUtils.EMPTY, user, OperateType.PASS.getIndex());
	}

	@Override
	public Page<Map<String, Object>> setPageTodoDataList(
			EasyuiPage<Map<String, Object>> page, Task filterTask) {
		String formTypeId = Constants.TASK_FORM_TYPE;
		return this.taskDao.findTodoTasks(page, filterTask, formTypeId);
	}
	
	@Override
	public int countTodoTasks(Task filterTask) {
		return this.taskDao.countTodoTasks(filterTask, Constants.TASK_FORM_TYPE);
	}

	@Override
	public Page<Map<String, Object>> setPageMyDataList(
			EasyuiPage<Map<String, Object>> page, Task filterTask) {
		String formTypeId = Constants.TASK_FORM_TYPE;
		return this.taskDao.findMyTasks(page, filterTask, formTypeId);
	}



	@Override
	public void invalid(User user, Long id, Long instanceId) {
		// 任务作废
		Task task = this.taskDao.get(id);
		task.setStatus(Constants.TASK_STATUS_INVALID);
		task.setInvalidUserId(user.getId());
		task.setInvalidUserName(user.getNickName());
		task.setInvalidTime(new Date());
		this.taskDao.saveOrUpdate(task);
		// 流程实例作废
		if (null != instanceId) {
			ProInstance instance = this.proInstanceDao.get(instanceId);
			instance.setStatus("4");
			this.proInstanceDao.save(instance);
		}
	}	

}
